//
//  LWTabBarViewController.swift
//  Microblog
//
//  Created by Roman on 17/1/4.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit

class LWTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let lwTabBar = LWTabBar()
        
        lwTabBar.composeClosure = { [weak self] in
            //如果访问到self就会有循环引用
            //添加微博的类型视图
            let compose = LWComposeView()
            //self?.view.addSubview(compose)
            compose.show(targetVC: self)
            //print(self!)
        }
        //设置自定义tabBar 只读属性不能够直接set,但是可以通过KVC简介赋值
        //self.tabBar = lwTabBar
        self.setValue(lwTabBar, forKey: "tabBar")
        addChildViewControllers()
    }
    //MARK: 添加子控制器
    func addChildViewControllers()  {
        
        addChildViewController(vc: LWHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(vc: LWDIscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 1)
        addChildViewController(vc: LWMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 2)
        addChildViewController(vc: LWProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
    }
    
    //MARK: 添加单个子控制器
    func addChildViewController(vc: UIViewController, title: String, imageName: String , index: Int)  {
        let nav = LWBaseController(rootViewController: vc)
        vc.title = title
        //设置tag
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置字体的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor.orange], for: UIControlState.selected)
        //统一设置字体的颜色
        //self.tabBar.tintColor = UIColor.orange
        
        //title 设置不同字
//        vc.tabBarItem.title = "首页"
//        vc.navigationItem.title = "haha"
        //设置tabBarItem 旁边的数字  badge -->徽章,象征
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeColor = UIColor.green
        
        //设置图片与文字的偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        //当图片和文字在一个图片上时使用
        //vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        addChildViewController(nav)
    }

    
    //通过协议方法来获取点击的底部Button
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print(item.tag)
        
        var index = 0
        
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
//                    var count: UInt32 = 0
//                    let ivarList = class_copyIvarList(subView.classForCoder, &count)!
                    
//                    //遍历ivarList获取单个ivar
//                    for i in 0..<count {
//                        let ivar = ivarList[Int(i)]
//                        //需要获取ivar 对应的名称和类型
//                        let ivarname = ivar_getName(ivar)
//                        //要将C语言字符串转换为swift的string
//                        let ivName = String(format: "%s", ivarname!)
//                        print(ivName)
//                        
//                        let ivType = ivar_getTypeEncoding(ivar)
//                        let typeName = String(format: "%s", ivType!)
//                        print(typeName)
//                    }
                    //print(subView)
                    //需要获取按钮中的imageView
                    //遍历按钮的子视图
                    for imageView in subView.subviews {
                        if imageView.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            print(imageView,"监控到相应的视图,可以开始做动画了")
                            imageView.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 3, options: [], animations: { 
                                imageView.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                print("OK")
                            })
                        }
                    }
    
                }
                index += 1
            }
            
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  

}
